package com.rtsffm.rtd.action.registry;

import com.rtsffm.rtd.action.controller.ActionDelegate;

//~--- JDK imports ------------------------------------------------------------


import java.lang.reflect.Method;

//~--- classes ----------------------------------------------------------------

class PSingleExecution implements PExecution {
    private final Class<?> declaredClass;
    private final Method   invokMethod;

    //~--- constructors -------------------------------------------------------

    public PSingleExecution(Method m) {
        invokMethod   = m;
        declaredClass = m.getDeclaringClass();
    }

    //~--- methods ------------------------------------------------------------

    @Override
    public boolean execute(final Object invoker, final Object cause, final Object context, Object... delegates) {
        if (invoker == null) {
            return true;
        }

        if (declaredClass.isAssignableFrom(invoker.getClass())) {
            if (!(invoker instanceof ActionDelegate) || ((ActionDelegate) invoker).isStillValid()) {
                try {
                    invokMethod.invoke(invoker, cause, context);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return true;
    }

    @Override
    public void addNextExecution(PExecution next) {}
}
